<?php
/** An interface to the Hosting Reborn API server functions.
 *
 * @package com\hostingreborn\api
 * @link https//api.hostingreborn.com/docs/functions/server/
 *
 * @author      Jon Cram <jon@hostingreborn.com>
 * @copyright   https://api.hostingreborn.com/code/copyright/
 * @license     https://api.hostingreborn.com/code/license/
 * @version     1.0
 *
 * $Date$
 * $Revision$
 *
 *
 */

require_once('HR_api.class.php');


class HR_serverApi extends HR_api {

    /***************************************************************************/
    /* Attributes                                                              */
    /***************************************************************************/

    protected $serverProperties = array(
            '/server/ip' => 'ip',
            '/server/hostname' => 'hostname',
            '/server/name' => 'name'
    );

    /***************************************************************************/
    /* Constructors                                                            */
    /***************************************************************************/

    /***************************************************************************/
    /* Public methods                                                          */
    /***************************************************************************/


    /**
     * @link http://api.hostingreborn.com/docs/functions/server/get/
     *
     * @param string $ip
     * @return array
     */
    public function get($ip = NULL, $key = null) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
                - server
                  - 100: server not found
        */

        $queryString = http_build_query(array(
                'server' => $ip,
                'key' => $key
        ));

        try {
            $response = $this->read('server/get/?'.$queryString);

            $server = $response->getCollection($this->serverProperties);

            foreach ($response->getSet('/server/nameservers/nameserver') as $nameserver) {
                if (!isset($server['nameservers'])) {
                    $server['nameservers'] = array();
                }
                
                $server['nameservers'][] = $nameserver->getValue('/nameserver');
            }
            
            return $server;
        } catch (HR_apiResponseUnhandledException $e) {
            $this->handleApiResponseUnhandledException($e);
        }

    }

    /***************************************************************************/
    /* Private methods                                                         */
    /***************************************************************************/

    /**
     *
     * @throws HR_apiServerException
     * @param HR_apiResponseUnhandledException $exception
     */
    private function handleApiResponseUnhandledException(HR_apiResponseUnhandledException $exception) {

        if ($exception->getApiExceptionType() == 'server') {
            switch ($exception->getApiExceptionCode()) {
                case '310':
                    $exceptionMessage = 'server not found';
                    break;
            }

            throw new HR_apiServerException($exceptionMessage, $exception->getApiExceptionCode());
        }
    }

}

?>